package divide;

public class ColumnDivider {

    public class DividingResult {

        private int dividend;
        private int divisor;
        private int result;
        private int rest;
        private int intermidiateValue;
        public final static int ARRAY_LENGTH = 20;
        private int[] dividentIntermidiateArray = new int[ARRAY_LENGTH];
        private int[] intermidiateValueArray = new int[ARRAY_LENGTH];
        private StringBuilder resultBuilder = new StringBuilder();
        private StringBuilder intermidiateValueBuilder = new StringBuilder();
        
        private DividingResult(int dividend, int divisor){
            this.dividend = dividend;
            this.divisor = divisor;
        }

        public int getResult() {
            return result;
        }

        public int getRest() {
            return rest;
        }

        public int getDividend() {
            return dividend;
        }

        public int getDivisor() {
            return divisor;
        }

        public int getIntermidiateValue() {
            return intermidiateValue;
        }

        public int[] getDividentIntermidiateArray() {
            return dividentIntermidiateArray;
        }

        public int[] getIntermidiateValueArray() {
            return intermidiateValueArray;
        }

        public StringBuilder getResultBuilder() {
            return resultBuilder;
        }

        public StringBuilder getIntermidiateValueBuilder() {
            return intermidiateValueBuilder;
        }
              
    }

    public DividingResult divide(int dividend, int divisor) {
        DividingResult dividingResult = new DividingResult(dividend, divisor);
        int arrayCount = 0;
        for (int count = 0; count < String.valueOf(dividend).length(); count++) {
            dividingResult.intermidiateValueBuilder
                    .append(String.valueOf(getNumber(dividend, count)));
            dividingResult.intermidiateValue = Integer.valueOf(dividingResult
                    .intermidiateValueBuilder.toString());
            if (dividingResult.intermidiateValue >= divisor) {
                dividingResult.resultBuilder.append(dividingResult
                        .intermidiateValue / divisor);
                dividingResult.intermidiateValueBuilder = new StringBuilder(
                        String.valueOf(dividingResult.intermidiateValue % divisor));
                dividingResult.dividentIntermidiateArray[arrayCount] =
                        dividingResult.intermidiateValue;
                dividingResult.intermidiateValueArray[arrayCount] =
                        (dividingResult.intermidiateValue / divisor) * divisor;
                arrayCount++;
            } else {
                dividingResult.resultBuilder.append(String.valueOf(0));
            }
        }
        try {
            dividingResult.result = Integer.valueOf(dividingResult
                    .resultBuilder.toString());
        } catch (NumberFormatException e) {
            dividingResult.result = 0;
        }
        dividingResult.rest = Integer.valueOf(dividingResult
                .intermidiateValueBuilder.toString());
        return dividingResult;
    }

    public static int getNumber(int value, int count) {
        String valueToString = String.valueOf(value);
        int number = Integer.valueOf(String.valueOf(valueToString.charAt(count)));
        return number;
    }
}
